<%--
 *  File:    ControlGLAccountBaseEdit.jsp
 *  @Author: Stephen Liu
 *  Date:    Friday, January 30, 2004

 *  Copyright (C) 2000 - 2001 Oval Technologies, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Oval
 *  Technologies, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Oval.
--%>

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.log4j.Category" %>
<%@ page import="com.oval.util.*" %>

<%@ page contentType ="text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/PageFlow.tld" prefix="PageFlow" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/custom.tld" prefix="custom" %>

<%
  Category cat = Category.getInstance("jsp.ControlGLAccountBaseEdit");
  String strModuleName = "GL";
  String privilegeCode = "40109";
%>

<%@ include file="/inc/PageDefault.inc" %>
<%@ include file="/inc/SessionTimeout.inc" %>
<%@ include file="/inc/Security.inc" %>

<html>
<head>
<title>controlglaccountbaseedit</title>
<PageFlow:CheckPageFlow pageName="controlglaccountbaseedit"/>
<%@ include file="/inc/InitEdit.inc" %>
<%
  HashMap glaccountinfo = (HashMap) pageContext.getAttribute("glaccountinfo");
  if (glaccountinfo == null)
    glaccountinfo = new HashMap();

  Vector allglaccountclasses = (Vector) pageContext.getAttribute("allglaccountclasses");
  if (allglaccountclasses == null)
    allglaccountclasses = new Vector();

  if (glaccountinfo.get("entitytype") != null && glaccountinfo.get("entitytype").toString().equals("1"))
    pageContext.setAttribute("glaccountlistid", glaccountinfo.get("entityid"));
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../css/<%= strLanguageCode %>/main.css" type="text/css">
<script language="JavaScript" src="../css/<%= strLanguageCode %>/script.js"></script>
<script language="JavaScript" src="../js/ScriptLib.js"></script>
<iframe name="reloadHandler" style="display:none" ></iframe>
<script language="JavaScript">
function page_save() {
  if (!checkForm(frm))
    return;
  formatCheckBox(frm);

  var glaccclassarr = frm.glaccountclasscode.options;
  var i = frm.glaccountclasscode.selectedIndex;
  if (i < glaccclassarr.length - 1 && glaccclassarr[i].value.length < glaccclassarr[i + 1].value.length) {
    alert("<%= message.getMessageDesc("GLACC_BE_CHILDPOINT") %>");
    frm.glaccountclasscode.focus();
    return;
  }

  if (frm.glaccountclasscode.value.charAt(0) == "5" && frm.profitandlosstype.value == -1) {
    alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_ProfitAndLossType")) %>");
    frm.profitandlosstype.focus();
    return;
  }
  if (frm.ledgerflag.checked && frm.ledgertype.value == -1) {
    alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_LedgerType")) %>");
    frm.ledgertype.focus();
    return;
  }
  if (frm.taxrelatedflag.checked && frm.taxtype.value == -1) {
    alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_TaxType")) %>");
    frm.taxtype.focus();
    return;
  }
//  if (frm.glassitemflag.checked && frm.assistantitemid.value == -1) {
//    alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_DefaultAssistantItem")) %>");
//    frm.assistantitemid.focus();
//    return;
//  }
  if (frm.foreigncurrencyflag0.checked && frm.currencytype2.checked && frm.forecurrencycode1.value == "-1") {
    alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_CommonForeCurrency")) %>");
    frm.forecurrencycode1.focus();
    return;
  }
  if (frm.foreigncurrencyflag1.checked && frm.forecurrencycode2.value == "-1") {
    alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_CurrencyType")) %>");
    frm.forecurrencycode2.focus();
    return;
  }

  if (frm.glaccountclasscode.value.charAt(0) != "5" && frm.profitandlosstype.disabled) {
    frm.profitandlosstype.disabled = false;
    frm.profitandlosstype.value = -1;
  }
  if (!frm.ledgerflag.checked && frm.ledgertype.disabled) {
    frm.ledgertype.disabled = false;
    frm.ledgertype.value = -1;
  }
  if (!frm.taxrelatedflag.checked && frm.taxtype.disabled) {
    frm.taxtype.disabled = false;
    frm.taxtype.value = -1;
  }
  if (!frm.glassitemflag.checked && frm.assistantitemid.disabled) {
    frm.assistantitemid.disabled = false;
    frm.assistantitemid.value = -1;
  }

  if (frm.foreigncurrencyflag0.checked && frm.currencytype2.checked)
    frm.forecurrencycode.value = frm.forecurrencycode1.value;
  else if (frm.foreigncurrencyflag1.checked)
    frm.forecurrencycode.value = frm.forecurrencycode2.value;
  else frm.forecurrencycode.value = "";

  if ("<%=JspHelper.filterNull( glaccountinfo.get("glaccountclasscode") )%>".charAt(0) != "5" && frm.glaccountclasscode.value.charAt(0) == "5")
    frm.profitandlosstypeinheritneed.value = "Y";
  if (Number("<%=JspHelper.filterNull( glaccountinfo.get("taxrelatedflag") )%>") == 0 && frm.taxrelatedflag.checked)
    frm.taxtypeinheritneed.value = "Y";

  frm.actionname.value = "save";
  isRunning = true;
  frm.submit();
}

function page_init() {
  with (frm) {

    if (!foreigncurrencyflag0.checked || !currencytype2.checked)
      forecurrencycode1.value = -1;
    if (!foreigncurrencyflag1.checked)
      forecurrencycode2.value = -1;

    if ("<%=JspHelper.filterNull( pageContext.getAttribute("isquoted") )%>" == "Y") {
      for (var i = 0; i < elements.length; i ++) {
        var element = elements[i];
        if (element.type == "select-one" || element.type == "text" || element.type == "checkbox" || element.type == "radio")
          element.disabled = true;
      }
      return;
    }

    var flag = flagcontainer.value;
    if (flag.length != 24)
      flag = "000000000000000000000000";
//    var case1 = (Number(fromtype.value) == 1);
    var case1 = false;  
    var case2 = (Number(fromtype.value) == 2 && Number(grade.value) > 1);
    var case12 = case1 || case2;

    var case12forledgertype = case1;

    dcflag.disabled = case1;
    referexchangeaccountcode.disabled = (case1 && flag.charAt(1) == "1") || case2;
    glaccountclasscode.disabled = case12;
    profitandlosstype.disabled = case1 || glaccountclasscode.value.charAt(0) != "5";
    ledgerflag.disabled = case12forledgertype;                           
    ledgertype.disabled = case12forledgertype || !ledgerflag.checked;
    taxrelatedflag.disabled = case12;
    taxtype.disabled = case1 || !taxrelatedflag.checked;
    glassitemflag.disabled = case12 && flag.charAt(6) == "1";
    assistantitemid.disabled = (case12 && flag.charAt(6) == "1") || !glassitemflag.checked;
    copassignflag.disabled = case12 && flag.charAt(7) == "1";
    createsubaccountflag.disabled = case1;

    if (case2 && flag.charAt(9) == "1") {
      foreigncurrencyflag0.disabled = true;
      foreigncurrencyflag1.disabled = true;
      currencytype1.disabled = true;
      currencytype2.disabled = true;
      forecurrencycode1.disabled = true;
      forecurrencycode2.disabled = true;
    }
    else {
      currencytype1.disabled = !foreigncurrencyflag0.checked;
      currencytype2.disabled = !foreigncurrencyflag0.checked;
      forecurrencycode1.disabled = !(foreigncurrencyflag0.checked && currencytype2.checked);
      forecurrencycode2.disabled = !foreigncurrencyflag1.checked;
    }
  }
}

function page_setGLAccount(glaccountid, accountname, accountcode, quickcode, grade, statusname, foreflag, isbottom, currencycode) {
  frm.referexchangeaccountid.value = glaccountid;
  frm.referexchangeaccountcode.value = accountcode;
}

function checkbox_change(flag, type) {
  if (!flag.checked) {
    type.value = -1;
    type.disabled = true;
  }
  else type.disabled = false;
}

function assignitemid(){
    if(document.frm.referexchangeaccountcode.value==""){
    frm.referexchangeaccountid.value=" ";
    return;
    }
    var  datahandler = new  DataHandler();
    datahandler.add_params("searchname","accountcode") ;
    datahandler.add_params("languagecode","cn") ;
    datahandler.add_params("booksettype","1","integer") ;
    datahandler.add_params("searchvalue" , eval("document.frm.referexchangeaccountcode").value) ;
    datahandler.getResultFromHashMap("glaccountsummary" ,"glaccountid" ,"document.frm.referexchangeaccountid");
    datahandler.add_job("getcontrolglaccountsummary") ;
    datahandler.runDataHandler ();
}
</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onload="javascript:set_contentleft_height1(420);page_init();">
<form name="frm" method="post" action="../Dispatcher">
  <input type="hidden" name="pagename" value="controlglaccountbaseedit">
  <input type="hidden" name="actionname">

  <custom:hidden property="glaccountid"/>
  <custom:hidden property="glaccountlistid"/>
  <custom:hidden property="currencycode"/>
  <custom:hidden name="glaccountinfo" property="forecurrencycode"/>
  <custom:hidden name="glaccountinfo" property="fromtype"/>
  <custom:hidden name="glaccountinfo" property="grade"/>
  <custom:hidden name="glaccountinfo" property="flagcontainer"/>

  <input type="hidden" name="profitandlosstypeinheritneed" value="N">
  <input type="hidden" name="taxtypeinheritneed" value="N">

  <table id="topTable" border=0 cellpadding=0 cellspacing=0 width="100%">
    <tr>
      <td valign=top>
        <table border=0 cellpadding=1 cellspacing=0 width="100%">
          <tbody>
          <tr>
            <td bgcolor=#000000 style="PADDING-BOTTOM: 1px; PADDING-LEFT: 1px; PADDING-RIGHT: 1px; PADDING-TOP: 0px">
              <table bgcolor=#000000 border=0 cellpadding=0 cellspacing=0 width="100%">
                <tr>
                  <td bgcolor=#ffffff style="PADDING-BOTTOM: 3px; PADDING-LEFT: 3px; PADDING-RIGHT: 3px; PADDING-TOP: 3px">
                   <div id=div2 style="HEIGHT: 245px; OVERFLOW: auto">
                      <table border="1" cellspacing="0" bordercolorlight="#999999" bordercolordark="#FFFFFF" align="center" width="100%" cellpadding="1">
                        <tr>
                          <td colspan="7" class="td_right">
                            <a onclick="javascript:page_save()"><custom:button type="2" label="L_Save" border="0"/></a>
                          </td>
                        </tr>
                        <tr>
                          <td width="110" class="need" height="20" align="right"><%= label.getLabelName("L_DCFlag") + label.getLabelName("L_Colon") %></td>
                          <td width="300" colspan="4">
                            <select name="dcflag">
                            <%
                              String selected1 = "";
                              String selected2 = "";
                              if ("1".equals(glaccountinfo.get("dcflag")))
                                selected1 = "selected";
                              else if ("2".equals(glaccountinfo.get("dcflag")))
                                selected2 = "selected";
                            %>
                              <option value="1" <%=JspHelper.filterNull( selected1 )%>><%= label.getLabelName("L_DebitFactor") %></option>
                              <option value="2" <%=JspHelper.filterNull( selected2 )%>><%= label.getLabelName("L_CreditFactor") %></option>
                            </select>
                          </td>
                          <td class="td-text" width="110"><%= label.getLabelName("L_ReferExchangeAccount") + label.getLabelName("L_Colon") %></td>
                          <td>
                            <custom:text name="glaccountinfo" property="referexchangeaccountcode" onchange="assignitemid();"/>
                            <a onclick="javascript:if(!referexchangeaccountcode.disabled)window.open('GLADebit.jsp?<%= OvalTools.randomURL() %>&orgid=<custom:write name="userinfo" property="orgid" scope="session"/>&needall=N','newwindow','height=400,width=600,top=50,left=50,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no')"><custom:button type="3" label="L_Asterisk" imgsrc="flash.gif" border="0"/></a>
                            <custom:hidden name="glaccountinfo" property="referexchangeaccountid"/>
                          </td>
                        </tr>
                        <tr>
                          <td class="need" height="20" align="right"><%= label.getLabelName("L_GLAccountAttribute") + label.getLabelName("L_Colon") %></td>
                          <td colspan="4">
                            <select name="glaccountclasscode" onchange="javascript:profitandlosstype.disabled=glaccountclasscode.value.charAt(0)!='5'">
                            <%
                              String glaccountclasscode = (String) glaccountinfo.get("glaccountclasscode");
                              if (glaccountclasscode == null)
                                glaccountclasscode = "";
                            %>
                            <logic:iterate id="oneglaccountclass" collection="<%= allglaccountclasses %>">
                            <%
                              HashMap hm = (HashMap) pageContext.getAttribute("oneglaccountclass");
                              String itemSelected = "";
                              String curclasscode = (String) hm.get("glaccountclasscode");
                              if (curclasscode != null && curclasscode.equalsIgnoreCase(glaccountclasscode))
                                itemSelected = "selected";
                              String indent = "";
                              Integer grade = (Integer) hm.get("grade");
                              if (grade != null)
                                for (int i = 1; i < grade.intValue(); i ++)
                                  indent += "&nbsp;";
                            %>
                            <option value="<%=JspHelper.filterNull( curclasscode )%>" <%=JspHelper.filterNull( itemSelected )%>><%=JspHelper.filterNull( indent )%><custom:write name="oneglaccountclass" property="glaccountclassname"/></option>
                            </logic:iterate>
                          </td>
                          <td class="td-text"><%= label.getLabelName("L_ProfitAndLossType") + label.getLabelName("L_Colon") %></td>
                          <td>
                            <custom:select name="glaccountinfo" property="profitandlosstype" optionsName="allprofitandlosstypes" optionValueKey="value" optionInnerTextKey="valuename" defaultValue="--------"/>
                          </td>
                        </tr>
                        <tr>
                          <td class="td-text" height="20" align="right"><%= label.getLabelName("L_LedgerFlag") + label.getLabelName("L_Colon") %></td>
                          <td colspan="4">
                            <custom:checkbox name="glaccountinfo" property="ledgerflag" checkFlag="1/0" style="border:0" onclick="javascript:ledgertype.disabled=!ledgerflag.checked"/>
                          </td>
                          <td class="td-text" align="right"><%= label.getLabelName("L_LedgerType") + label.getLabelName("L_Colon") %></td>
                          <td>
                            <custom:select name="glaccountinfo" property="ledgertype" optionsName="allledgertypes" optionValueKey="value" optionInnerTextKey="valuename" defaultValue="--------"/>
                          </td>
                        </tr>
                        <tr>
                          <td class="td-text" height="20"><%= label.getLabelName("L_TaxRelatedFlag") + label.getLabelName("L_Colon") %></td>
                          <td colspan="4">
                            <custom:checkbox name="glaccountinfo" property="taxrelatedflag" checkFlag="1/0" style="border:0" onclick="javascript:taxtype.disabled=!taxrelatedflag.checked"/>
                          </td>
                          <td class="td-text"><%= label.getLabelName("L_TaxType") + label.getLabelName("L_Colon") %></td>
                          <td>
                            <custom:select name="glaccountinfo" property="taxtype" optionsName="alltaxtypes" optionValueKey="value" optionInnerTextKey="valuename" defaultValue="--------"/>
                          </td>
                        </tr>
                        <tr>
                          <td class="td-text" height="20" align="right"><%= label.getLabelName("L_GLAssItemFlag") + label.getLabelName("L_Colon") %></td>
                          <td colspan="4">
                           <custom:checkbox name="glaccountinfo" property="glassitemflag" checkFlag="1/0" style="border:0" onclick="assistantitemid.disabled=!glassitemflag.checked;if(glassitemflag.checked){copassignflag.checked=true;}"/>
                          </td>
                          <td class="td-text"><%= label.getLabelName("L_DefaultAssistantItem") + label.getLabelName("L_Colon") %></td>
                          <td>
                            <custom:select name="glaccountinfo" property="assistantitemid" optionsName="allassistantitems" optionValueKey="assistantitemid" optionInnerTextKey="itemname" defaultValue="--------"/>
                          </td>
                        </tr>
                        <tr>
                          <td class="td-text" height="20" align="right"><%= label.getLabelName("L_CashflowItemFlag") + label.getLabelName("L_Colon") %></td>
                          <td colspan="4">
                           <custom:checkbox name="glaccountinfo" property="cashflowitemflag" checkFlag="1/0" style="border:0" />
                          </td>
                          <td class="td-text">&nbsp;</td>
                          <td>&nbsp;</td>
                        </tr>
                        <tr>
                          <td class="td-text" height="20"><%= label.getLabelName("L_CreateSubAccountFlag") + label.getLabelName("L_Colon") %></td>
                          <td colspan="4">
                            <custom:checkbox name="glaccountinfo" property="createsubaccountflag" checkFlag="1/0" style="border:0"/>
                          </td>
                          <td class="td-text"><%= label.getLabelName("L_COPAssignFlag") + label.getLabelName("L_Colon") %></td>
                          <td>
                            <custom:checkbox name="glaccountinfo" property="copassignflag" checkFlag="1/0" style="border:0" onclick="if(glassitemflag.checked){copassignflag.checked=true;}"/>
                          </td>
                        </tr>
                        <tr>
                          <%
                            String flag0 = "checked";
                            String flag1 = "";
                            if ("1".equals(glaccountinfo.get("foreigncurrencyflag"))) {
                              flag0 = "";
                              flag1 = "checked";
                            }

                            String type1 = "checked";
                            String type2 = "";
                            if (glaccountinfo.get("currencytype") != null && "2".equals(glaccountinfo.get("currencytype").toString())) {
                              type1 = "";
                              type2 = "checked";
                            }
                          %>
                          <td class="td-text" rowspan="3"><%= label.getLabelName("L_CurrencyAttribute") + label.getLabelName("L_Colon") %></td>
                          <td rowspan="2" width="10">
                            <input type="radio" name="foreigncurrencyflag" id="foreigncurrencyflag0" value="0" class="normalradio" <%=JspHelper.filterNull( flag0 )%> onclick="javascript:currencytype1.disabled=false;currencytype2.disabled=false;forecurrencycode1.disabled=!currencytype2.checked;forecurrencycode2.disabled=true;if(!currencytype1.checked&&!currencytype2.checked)currencytype1.checked=true;">
                          </td>
                          <td rowspan="2" width="110"><%= label.getLabelName("L_LocalCurrency") + label.getLabelName("L_GLAccount") %></td>
                          <td width="10">
                            <input type="radio" name="currencytype" id="currencytype1" value="1" class="normalradio" <%=JspHelper.filterNull( type1 )%> onclick="javascript:forecurrencycode1.disabled=true;">
                          </td>
                          <td width="126"><%= label.getLabelName("L_SingleLocalCurrency") %></td>
                          <td width="110">&nbsp;
                          </td>
                          <td>&nbsp;</td>
                        </tr>
                        <tr>
                          <td>
                            <input type="radio" name="currencytype" id="currencytype2" value="2" class="normalradio" <%=JspHelper.filterNull( type2 )%> onclick="javascript:forecurrencycode1.disabled=false;">
                          </td>
                          <td><%= label.getLabelName("L_MultiForeignCurrency") %></td>
                          <td>
                            <div align="right"><%= label.getLabelName("L_CommonForeCurrency") + label.getLabelName("L_Colon") %></div>
                          </td>
                          <td>
                            <custom:select name="glaccountinfo" property="forecurrencycode" changedName="forecurrencycode1" optionsName="allcurrencies" optionValueKey="currencycode" optionInnerTextKey="currencyname" defaultValue="------"/>
                          </td>
                        </tr>
                        <tr>
                          <td>
                            <input type="radio" name="foreigncurrencyflag" id="foreigncurrencyflag1" value="1" class="normalradio" <%=JspHelper.filterNull( flag1 )%> onclick="javascript:currencytype1.disabled=true;currencytype2.disabled=true;forecurrencycode1.disabled=true;forecurrencycode2.disabled=false;">
                          </td>
                          <td><%= label.getLabelName("L_ForeignCurrency") + label.getLabelName("L_GLAccount") %></td>
                          <td>&nbsp;</td>
                          <td><%= label.getLabelName("L_SingleForeignCurrency") %></td>
                          <td>
                            <div align="right"><%= label.getLabelName("L_CurrencyType") + label.getLabelName("L_Colon") %></div>
                          </td>
                          <td>
                            <custom:select name="glaccountinfo" property="forecurrencycode" changedName="forecurrencycode2" optionsName="allcurrencies" optionValueKey="currencycode" optionInnerTextKey="currencyname" defaultValue="------"/>
                          </td>
                        </tr>
                      </table>
                    </div>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
      <td class=summary_top></td>
    </tr>
    <tr>
      <%@ include file="ControlGLAccountEditBtn.inc" %>
    </tr>
  </table>
</form>
</body>
</html>
